import React, { useState } from 'react'

interface IProps {
  name: string;
}
/**
 * @title Checkbox 多选框
 * @param props 
 */
export default function GmCheckbox(props:IProps) {
  const {name='name'} = props;
  const [isChecked, setChecked] = useState<boolean>(false);
  const handleCheck = () => {
    setChecked(!isChecked)
  }

  return (
    <div className="gm-checkbox">
      <input type="checkbox" id={name} checked={isChecked} onChange={handleCheck} />
      <label htmlFor={name}>Awesome</label>
      <style jsx>{`
        /* 隐藏 input[type="checkbox"] -- 原理label绑定了input */
        .gm-checkbox input[type="checkbox"] {
          position: absolute;
          clip: rect(0, 0, 0, 0);
        }
        
        .gm-checkbox input[type="checkbox"] + label {
          cursor: pointer;
          position: relative;
          line-height: 12px;
          user-select: none;
        }
        .gm-checkbox input[type="checkbox"]:checked + label{
          color: #b4a078;
        }
        .gm-checkbox input[type="checkbox"]:disabled + label {
          cursor: not-allowed;
          color: #999;
        }

        /* before为框,after为勾号 */
        .gm-checkbox input[type="checkbox"] + label::before{
          content: "";
          display: inline-block;
          width: 12px; height: 12px;
          border-radius: 2px;
          /* vertical-align: top; */
          margin-right: .4em;
          border: 1px solid #ccc;
          background-color: #fff;
          transition: border-color .2s ease-in-out, background-color .2s ease-in-out;
        }
        .gm-checkbox input[type="checkbox"]:not(:disabled) + label:hover::before{
          border-color: #b4a078;
        }
        .gm-checkbox input[type="checkbox"] + label::after{
          content: "";
          display: inline-block;
          width: 4px; height: 7px;
          border: 1.5px solid #fff;
          border-top: 0;
          border-left: 0;
          position: absolute;
          left: 4px; top: 5px;
          transform: rotate(45deg) scale(0);
          transition: all .2s ease-in-out;
        }

        .gm-checkbox input[type="checkbox"]:checked + label::before{
          border-color: #b4a078 !important;
          background-color: #b4a078;
        }
        .gm-checkbox input[type="checkbox"]:checked + label::after{
          transform: rotate(45deg) scale(1);
          transition: all .2s ease-in-out;
        }
        .gm-checkbox input[type="checkbox"]:disabled + label::before,
        .gm-checkbox input[type="checkbox"]:disabled.checked + label::before{
          background-color: #f2f2f2;
        }
        .gm-checkbox input[type="checkbox"]:disabled.checked + label::after{
          border-color: #ccc;
          transform: rotate(45deg) scale(1);
        }
      `}</style>
    </div>
  )
}
